<template>
  <page-dialog
    :title="dialogInfo.title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <page-table
      v-model:data="tableInfo.data"
      :field-list="tableInfo.fieldList"
      :handle="tableInfo.handle"
      :highlight-row="highlightRow"
      :check-box="checkBox"
      :pager="false"
      @handleEvent="handleEvent"
    >
      <template v-slot:col-licenseNumber="scope">
        <p>{{ scope.row.licenseNumber || "-" }}</p>
        <p>{{ scope.row.brandName || "-" }}</p>
      </template>
      <template v-slot:col-commonName="scope">
        <p>{{ scope.row.commonName || "-" }}</p>
        <p>{{ scope.row.productCode || "-" }}</p>
        <p>{{ scope.row.alias || "-" }}</p>
      </template>
      <template v-slot:col-type="scope">
        <p>{{ scope.row.productTypeValue || "-" }}</p>
        <p>{{ scope.row.codeTypeValue || "-" }}</p>
        <p>{{ scope.row.productCategoryValue || "-" }}</p>
      </template>
      <template v-slot:col-standard="scope">
        <p>{{ scope.row.standard || "-" }}</p>
        <p>{{ scope.row.unitName || "-" }}</p>
      </template>
    </page-table>
  </page-dialog>
</template>
<script setup name="SelectProductDialog">
import PageDialog from "@/components/PageDialog/index";
import PageTable from "@/components/PageTable/index";

const { proxy } = getCurrentInstance();
const emit = defineEmits(["update", "callback", "reset"]);
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {};
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
});
const shows = computed({
  get() {
    return props.showDialog;
  },
  set(newValue) {
    emit("update:showDialog", newValue); // 更新父组件shows
  },
});

// 过滤相关配置
// 表格相关
const tableInfo = reactive({
  data: [],
  fieldList: [
    {
      label: "通用名称\n编码\n别名",
      value: "commonName",
      minWidth: 220,
      type: "slot",
    },
    {
      label: "产品类型\n条码类型\n产品分类",
      value: "type",
      minWidth: 150,
      type: "slot",
    },
    { label: "规格\n计量单位", value: "standard", type: "slot" },
    {
      label: "证号\n生产厂家",
      value: "licenseNumber",
      minWidth: 220,
      type: "slot",
    },
  ],
});
// 弹窗相关
const dialogInfo = reactive({
  title: "选择产品",
  width: "70%",
  btLoading: false,
  btList: [
    { label: "关闭", event: handleClose, show: true },
    {
      label: "保存",
      type: "primary",
      disabled: true,
      event: save,
      show: true,
    },
  ],
});
const selectRow = ref("");

onMounted(() => {
  proxy.$nextTick(() => {
    tableInfo.data = props.query.data;
  });
});

function save () {
  // emit('input', data)
  if (selectRow.value) {
    handleClose();
    emit("callback", selectRow.value);
  } else {
    proxy.$message({
      type: "warning",
      duration: 3500,
      showClose: true,
      message: "请选择产品",
    });
  }
};
function handleEvent (event, data) {
  switch (event) {
    case "tableClickRow":
      selectRow.value = data;
      dialogInfo.btList[1].disabled = !selectRow.value;
      break;
    case "tableCheck":
      if (data.length) {
        selectRow.value = data;
        dialogInfo.btList[1].disabled = !selectRow.value;
      }
      break;
  }
};
// 关闭弹窗前的回调
function handleClose () {
  shows.value = false;
  emit("reset");
};
</script>
